import React from "react";
import {Select} from "antd";
import BaseComponent from "../../../redare/components/BaseComponent";
import {ifTrue} from "../../../redare/utils/Utils";
import ReactDOM from "react-dom";
import DictionaryPresenter from "../../../.gen/presenter/DictionaryPresenter";

export default class MultipleDicSelect extends BaseComponent {

    static defaultProps = {
        style: undefined
    };

    constructor(props) {
        super(props);
        this.state = {
            loading: true,
            data: []
        };
        this.dictPresenter = new DictionaryPresenter(this);
    }

    componentDidMount() {
        this._findDictionaryList();
    }

    _findDictionaryList() {
        let {code} = this.props;
        if (!code) {
            return;
        }
        this.dictPresenter.findDictionaryList(
            {parentCode: code},
            {},
            (success_re) => {
                setTimeout(() => {
                    let data = success_re.data;
                    this.setState({loading: false, data: success_re.data});
                    this._loadValueForComma();
                }, 500);
            },
            (fail_re) => {
                this.setState({loading: false});
            }
        );
    }

    _loadValueForComma() {
        let data = this.state.data;
        let selectOptionKeyObj = this.selectOptionKeyObj;
        let valueArray = [];

        let {matchkey} = this.props;
        matchkey = matchkey ? matchkey : 'id';

        if (data && data.length > 0 && selectOptionKeyObj) {
            data.map((item) => {
                if (selectOptionKeyObj[item[matchkey]]) {
                    valueArray.push({
                        label: item.dicName,
                        value: item.dicCode
                    })
                }
            });
            this._onDicValueChange(valueArray);
        }
    }

    getMatchKeyList() {
        let result = null;
        let value = this.state.value;
        if (value && value.length > 0) {
            result = [];
            value.map((item) => {
                result.push(item["value"]);
            });
        }
        return result;
    }

    resetData() {
        this.setState({value: undefined});
    }

    // 传递一个以逗号分割的code 列
    setValueForComma(value) {
        if (value && value.length > 0) {
            let paramArray = value.split(",");
            let paramObj = {};
            paramArray.map((item) => {
                paramObj[item] = item;
            });
            this.selectOptionKeyObj = paramObj;
            this._loadValueForComma();
        }
    }

    // 传递code数组
    setValueForArray(paramArray) {
        if (!paramArray || paramArray.length <= 0) {
            return;
        }
        let paramObj = {};
        paramArray.map((item) => {
            paramObj[item] = item;
        });
        this.selectOptionKeyObj = paramObj;
        this._loadValueForComma();
    }

    setDicValue(value) {
        this._onDicValueChange(value)
    }

    _onDicValueChange(value) {
        if (ifTrue(value)) {
            if (!(value instanceof Array)) {
                value = [value];
            }
        }

        value.map((item) => {
            item.code = item.value;
            item.name = item.label;
        });

        this.setState({value});

        if (this.props.onChange) {
            this.props.onChange(value);
        }
    }

    _renderSelectOption() {
        let options = [];
        let {matchkey} = this.props;
        matchkey = matchkey ? matchkey : 'id';
        this.state.data.forEach((v) => {
            options.push(
                <Select.Option key={"option_" + v.id} value={v[matchkey]}>{v.dicName}</Select.Option>
            )
        });
        return options;
    }


    render() {
        let {loading, value} = this.state;
        return (
            <div ref={(o) => this.dicSelect = o}>
                <Select
                    mode="multiple"
                    placeholder="请选择"
                    allowClear={true}
                    labelInValue={true}
                    style={this.props.style}
                    value={value}
                    loading={loading}
                    getPopupContainer={() => ReactDOM.findDOMNode(this.dicSelect)}
                    onChange={this._onDicValueChange.bind(this)}
                >
                    {
                        this._renderSelectOption()
                    }
                </Select>
            </div>
        );
    }
}
